//you can create partial mock with spy() method: List list = spy(new LinkedList());
//you can enable partial mock capabilities selectively on mocks: Foo mock = mock(Foo.class); //Be sure the real implementation is 'safe'. //If real implementation throws exceptions or depends on specific state of the object then you're in trouble. when(mock.someMethod()).thenCallRealMethod();
[17] Changing default return values of unstubbed invocations (Since 1.7)
Changing default return values of unstubbed invocations (Since 1.7)
List mock = mock(List.class); when(mock.size()).thenReturn(10); mock.add(1);
reset(mock); //at this point the mock forgot any interactions & stubbing
[24] One-liner stubs (Since 1.9.0)
One-liner stubs (Since 1.9.0)
1 2 3 4 5
publicclassCarTest{ Car boringStubbedCar = when(mock(Car.class).shiftGear()).thenThrow(EngineNotStarted.class).getMock();
@Testpublicvoid should... {} }
Matchers 類
[3] Argument matchers
Argument matchers
1 2 3 4 5 6 7 8 9 10 11
//stubbing using built-in anyInt() argument matcher when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher): when(mockedList.contains(argThat(isValid()))).thenReturn("element");
//following prints "element" System.out.println(mockedList.get(999));
//you can also verify using an argument matcher verify(mockedList).get(anyInt());
如果使用 matcher 的話,每一個都必須用 matcher
1 2 3 4 5
verify(mock).someMethod(anyInt(), anyString(), eq("third argument")); //above is correct - eq() is also an argument matcher
verify(mock).someMethod(anyInt(), anyString(), "third argument"); //above is incorrect - exception will be thrown because third argument is given without an argument matcher.
Verify 類
[1] Let’s verify some behaviour!
[4] Verifying exact number of invocations / at least x / never
[6] Verification in order(驗證操作順序)
[7] Making sure interaction(s) never happened on mock(驗證無任何互動)
[8] Finding redundant invocations(驗證所有互動是否皆驗證到)
[15] Capturing arguments for further assertions (Since 1.8.0)
[22] Verification with timeout (Since 1.8.5)
[25] Verification ignoring stubs (Since 1.9.0)
Verify 類說明
[1] Let’s verify some behaviour!
Let's verify some behaviour!
1 2 3 4 5 6 7 8 9 10
//mock creation List mockedList = mock(List.class);
//following two verifications work exactly the same - times(1) is used by default verify(mockedList).add("once"); verify(mockedList, times(1)).add("once");
//exact number of invocations verification verify(mockedList, times(2)).add("twice"); verify(mockedList, times(3)).add("three times");
//verification using never(). never() is an alias to times(0) verify(mockedList, never()).add("never happened");
//verification using atLeast()/atMost() verify(mockedList, atLeastOnce()).add("once"); verify(mockedList, atLeast(2)).add("twice"); verify(mockedList, atMost(5)).add("three times");
// A. Single mock whose methods must be invoked in a particular order List singleMock = mock(List.class);
//using a single mock singleMock.add("was added first"); singleMock.add("was added second");
//create an inOrder verifier for a single mock InOrder inOrder = inOrder(singleMock);
//following will make sure that add is first called with "was added first, then with "was added second" inOrder.verify(singleMock).add("was added first"); inOrder.verify(singleMock).add("was added second");
// B. Multiple mocks that must be used in a particular order List firstMock = mock(List.class); List secondMock = mock(List.class);
//using mocks firstMock.add("was called first"); secondMock.add("was called second");
//create inOrder object passing any mocks that need to be verified in order InOrder inOrder = inOrder(firstMock, secondMock);
//following will make sure that firstMock was called before secondMock inOrder.verify(firstMock).add("was called first"); inOrder.verify(secondMock).add("was called second");
// Oh, and A + B can be mixed together at will
[7] Making sure interaction(s) never happened on mock(驗證無任何互動)
Making sure interaction(s) never happened on mock
1 2 3 4 5 6 7 8 9 10 11
//using mocks - only mockOne is interacted mockOne.add("one");
classPerson{ Person doSomething(Person abc){ return abc; } }
[22] Verification with timeout (Since 1.8.5)
Verification with timeout (Since 1.8.5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//passes when someMethod() is called within given time span verify(mock, timeout(100)).someMethod(); //above is an alias to: verify(mock, timeout(100).times(1)).someMethod();
//passes when someMethod() is called *exactly* 2 times within given time span verify(mock, timeout(100).times(2)).someMethod();
//passes when someMethod() is called *at least* 2 times within given time span verify(mock, timeout(100).atLeast(2)).someMethod();
//verifies someMethod() within given time span using given verification mode //useful only if you have your own custom verification modes. verify(mock, new Timeout(100, yourOwnVerificationMode)).someMethod();
[25] Verification ignoring stubs (Since 1.9.0)
Verification ignoring stubs (Since 1.9.0)
1 2 3 4 5 6 7 8 9 10 11
verify(mock).foo(); verify(mockTwo).bar();
//ignores all stubbed methods: verifyNoMoreInvocations(ignoreStubs(mock, mockTwo));
//creates InOrder that will ignore stubbed InOrder inOrder = inOrder(ignoreStubs(mock, mockTwo)); inOrder.verify(mock).foo(); inOrder.verify(mockTwo).bar(); inOrder.verifyNoMoreInteractions();
BDD
[19] Aliases for behavior driven development (Since 1.8.0)
[29] (new) BDD style verification (Since 1.10.0)
Aliases for behavior driven development (Since 1.8.0)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
importstatic org.mockito.BDDMockito.*;
Seller seller = mock(Seller.class); Shop shop = new Shop(seller);
@InjectMocks Person person = new Person(); @InjectMocks Person person1 = mock(Person.class); @InjectMocks Person person2 = spy(new Person()); //@InjectMocks @Spy Person person3; @Spy Person person4; @Mock Person person5;